<link rel="stylesheet" href="css/chosen.css" />
<script src="js/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
		$("#tgl_kunjungan").datepicker({
			dateFormat  : "yy-mm-dd",        
			  changeMonth : true,
			  changeYear  : true					
		});
		
		$("#id_customer").change(function(){
			$.ajax({						
				url:'getCus.php',
				dataType: "html",
				type:'POST',
				data:'id_customer='+$(this).val(),
				success: function(data) {
					var obj = $.parseJSON(data);						
					if(data=="[]"){
						$("#kunjungan").val('Empty');
						$("#remainder").val('Empty');		
					}else{							
						$("#kunjungan").val(obj[0].kunjungan);
						$("#remainder").val(obj[0].remainder);
					}
				}
			})

		})
	});
</script>
<?php
if($_SESSION['jadwalatur']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Jadwal</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=jadwal" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="jadwal">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="tgl_jual")?"selected":"";?>>Tanggal</option>
									<option value="b.nama" <?php echo ($field=="b.nama")?"selected":"";?>>Seles</option>
									<option value="c.nama" <?php echo ($field=="c.nama")?"selected":"";?>>Customer</option>
									<option value="remainder" <?php echo ($field=="remainder")?"selected":"";?>>Remainder</option>									
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Tanggal Kunjungan</th>
									<th class="left">Seles</th>
									<th class="left">Customer</th>
									<th>Reminder</th>
									<th>Status</th>
									<th class="action" colspan="2"><a href="?pages=jadwal&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlKode1 = mysql_query("SELECT id_schedule,tgl_jual,b.nama as nama_seles,c.nama as nama_customer,remainder,a.status FROM tb_schedule a
LEFT JOIN tb_sales b
ON a.id_sales=b.id_sales
LEFT JOIN tb_customer c
ON a.id_customer=c.id_customer $addQuery ORDER BY tgl_jual DESC limit $posisi, $batas");
							
							$no=$posisi+1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								$status = ($dataKode['status']==1)? "Dikunjungi":"Belum";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataKode['tgl_jual'];?></td>
									<td><?php echo $dataKode['nama_seles'];?></td>
									<td><?php echo $dataKode['nama_customer'];?></td>
									<td><?php echo $dataKode['remainder'];?></td>
									<td><?php echo $status;?></td>
									<td class="action"><a href="?pages=jadwal&act=edit&id=<?php echo $dataKode['id_schedule'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=jadwal&act=del&id=<?php echo $dataKode['id_schedule'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT id_schedule,tgl_jual,b.nama as nama_seles,c.nama as nama_customer,remainder FROM tb_schedule a
LEFT JOIN tb_sales b
ON a.id_sales=b.id_sales
LEFT JOIN tb_customer c
ON a.id_customer=c.id_customer $addQuery ORDER BY tgl_jual DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=jadwal&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_kode = (int) filter($_GET['id']);

	$sqlKode = mysql_query("SELECT * FROM tb_schedule a,tb_customer b WHERE a.id_customer=b.id_customer AND id_schedule=$id_kode");
	$dataKode = mysql_fetch_array($sqlKode);

	if(!empty($id_kode)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Jadwal Kunjungan </h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Jadwal Kunjungan</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=jadwal&act=update">
		<input type="hidden" name="id_kode" value="<?php echo $dataKode['id_schedule'];?>">
		<table style="width:auto">
		   	<tbody>
		   		
				<tr>
					<td style="vertical-align: top;"><label>Nama Customer</label></td>
					<td>:</td>
					<td>
						<select name="id_customer" id="id_customer" data-placeholder="Pilih Nama Customer" style="width: 210px;">
						<option value=""></option>
					<?php 
					$sqlBB = mysql_query("SELECT * FROM tb_customer");
					while($dataBB = mysql_fetch_array($sqlBB)){
						if($dataBB['id_customer']==$dataKode['id_customer']){
							$selected = " selected ";
						}else{
							$selected = "";
						}
						echo '<option value="'.$dataBB['id_customer'].'" '.$selected.'>'.$dataBB['nama'].'</option>';
					}
					?>
					</select><input type="hidden" name="nama_bb" id="nama_bb" />
			   	</tr>
				<tr>
					<td><label>Kunjungan Terakhir</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['kode_bb'];?>" size="30" name="kunjungan" id="kunjungan" placeholder="Tanggal Kunjungan" readonly="true"></td>
			   	</tr>
				<tr>
					<td><label>Remainder</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['remainder'];?>" size="30" name="remainder" id="remainder" placeholder="Remainder" readonly="true"></td>
			   	</tr>
				<tr>
					<td><label>Tanggal Kunjungan</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['tgl_jual'];?>" size="60" name="tgl_kunjungan" id="tgl_kunjungan" placeholder="Tanggal" required></td>
			   	</tr>
				<tr>
					<td><label>Nama Sales</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_sales" id="id_sales" data-placeholder="Pilih Nama Sales"  required>
                      <option ></option>
                      
                      <?php
						$select_jur = mysql_query("SELECT * FROM tb_sales");
						while($select_jur2 = mysql_fetch_array($select_jur)){
							if($select_jur2['id_sales']==$dataKode['id_sales']){
								$selected = " selected ";
							}else{
								$selected = "";
							}
                      ?>
                      <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_sales]";?>"><?php echo"$select_jur2[nama]";?></option>
                      <?php
                            }
                      ?>
                        </select>
			   	</tr>
				<tr>
					<td><label>Status</label></td>
					<td>:</td>
				 	<td>
				 		<select name="status" style="width:202px">
							<option value="1" <?php echo $status = ($dataSale['status']==1)? "selected":"";?>>Dikunjungi</option>
							<option value="0" <?php echo $status = ($dataSale['status']==0)? "selected":"";?>>Belum</option>
						</select>
				 	</td> 
			   	</tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_kode = (int) $_POST['id_kode'];
	$id_sales = $_POST['id_sales'];
	$id_customer = $_POST['id_customer'];
	$tgl_kunjungan = $_POST['tgl_kunjungan'];
	$status = $_POST['status'];
	

	if(!empty($id_kode)){
		$update = mysql_query("UPDATE tb_schedule SET id_sales='$id_sales', id_customer='$id_customer', tgl_jual='$tgl_kunjungan', status='$status' WHERE id_schedule=$id_kode");
	
		if($update)
			sukses('Data berhasil disimpan', '?pages=jadwal');
		else
			sukses('Data gagal disimpan', '?pages=jadwal');
			
	}else{
		$insert = mysql_query("INSERT INTO tb_schedule VALUES(NULL, '$id_sales', '$id_customer', '$tgl_kunjungan',0)");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=jadwal');
		else
			sukses('Data gagal disimpan', '?pages=jadwal');
	}
	
}elseif ($act=='del') {
	# code...
	$id_kode = (int) filter($_GET['id']);

	$delete = mysql_query("DELETE FROM tb_schedule WHERE id_schedule=$id_kode");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=jadwal');
}
?>	
<script type="text/javascript">
$(document).ready(function () {
	$("#id_customer, #id_sales").chosen();  
})
</script>					